package jwd.base

import java.io.InputStream
import java.lang.reflect.Field
import java.math.BigDecimal
import java.nio.charset.Charset
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by udi on 17-7-30.
 */

fun InputStream?.GetString(): String {
    if( this == null) return ""
    return String(this.readBytes(), Charset.defaultCharset())
}

fun Class<*>.IsSimpleClass(): Boolean {
    if (this.isPrimitive) return true;
    if (this.isEnum) return true;
    if (this.name == "java.lang.String") return true;
    if (this.name == "java.util.Date") return true;
    if (this.name == "org.bson.types.ObjectId") {
        return true;
    }
    return false;
}


fun Date.ToFormatString(format: String = ""): String {
    var format = format;
    if (format.isEmpty()) {
        format = "yyyy-MM-dd HH:mm:ss";
    }

    var dateFormat = SimpleDateFormat(format)
    return dateFormat.format(this);
}


val Class<*>.allFields: List<Field>
    get() {
        var ret = mutableListOf<Field>();
        ret.addAll(this.declaredFields)

        if (this.superclass == null || this.superclass == Any::class.java) {
            return ret;
        }
        ret.addAll(this.superclass.allFields)
        return ret;
    }

fun MutableList<*>.RemoveRange(startIndex: Int, endIndex: Int) {
    var startIndex = startIndex
    var endIndex = endIndex

    var tmp = 0
    if (startIndex > endIndex) {
        tmp = startIndex
        startIndex = endIndex
        endIndex = tmp
    }

    for (i in startIndex..endIndex) {
        this.removeAt(startIndex)
    }
}

fun <T> MutableList<T>.Swap(index1: Int, index2: Int) {
    var tmp = this[index1];
    this[index1] = this[index2]
    this[index2] = tmp;
}


fun Any?.AsInt(defaultValue: Int = 0): Int {
    if (this == null) return defaultValue;
    try {
        if (this is Int) return this;
        if (this is String) {
            if (this.isEmpty()) return defaultValue
            return this.toInt();
        }
        if (this is Float) return this.toInt();
        if (this is Long) return this.toInt();
        if (this is Double) return this.toInt();
        if (this is BigDecimal) return this.toInt();
        if (this is Byte) return this.toInt();
        if (this is Short) return this.toInt();


        return defaultValue;
    } catch(e: Exception) {
        return defaultValue;
    }
    return defaultValue;
}



fun <T> Array<out T>.slice(startIndex: Int, endIndex: Int = 0): List<T> {
    var startIndex = startIndex
    var endIndex = endIndex;

    if (endIndex <= 0) {
        endIndex = this.size + endIndex
    }

    if (startIndex < 0) {
        startIndex = this.size + startIndex
    }
    if (endIndex < startIndex) return listOf()
    if (startIndex >= this.size) return listOf()
    if (endIndex >= this.size) {
        endIndex = this.size - 1
    }

    return this.slice(startIndex..endIndex)
}

fun String.slice(startIndex: Int, endIndex: Int = 0): String {
    var startIndex = startIndex
    var endIndex = endIndex;

    if (endIndex <= 0) {
        endIndex = this.length + endIndex
    }

    if (startIndex < 0) {
        startIndex = this.length + startIndex
    }
    if (endIndex < startIndex) return ""
    if (startIndex >= this.length) return ""
    if (endIndex >= this.length) {
        endIndex = this.length - 1
    }
    return this.slice(startIndex..endIndex)
}
